//
//  RHAlertView.m
//  RuiHeAppApp
//
//  Created by Yan Chen on 2024/6/8.
//

#import "RHAlertView.h"
@interface RHAlertView()
@property (nonatomic ,copy) NSString *titleStr;

@property (nonatomic ,copy) NSString *subTitleStr;

@property (nonatomic ,assign)RHAlertType type;

@property (nonatomic, strong) QMUITextView *textView;

@property (nonatomic, copy) void(^cancelBlock)(void);

@property (nonatomic, strong) QMUILabel *numberLab;
@end
@implementation RHAlertView

-(instancetype)initWithType:(RHAlertType)type title:(NSString*)title subTitle:(NSString*)subTitle
{
    if (self = [super initWithFrame:CGRectMake(0, 0, kUI_WINDOW_W, kUI_WINDOW_H)]) {
        _titleStr = title;
        _subTitleStr = subTitle;
        _type = type;
        _textMaxLengh = 64;
        [self setupUI];
    }
    return self;
}

-(void)setupUI
{
    kWeakSelf;
    UIView *grayView = kView;
    grayView.backgroundColor = kCOLOR_HEX_A(@"000000", 0.3);
    [self addSubview:grayView];
    [grayView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.edges.insets(UIEdgeInsetsZero);
    }];
    
    UIView *contentView = kView;
    contentView.backgroundColor = CFTabViewBgColor;
    contentView.layer.cornerRadius = 8;
    contentView.layer.masksToBounds = YES;
    [self addSubview:contentView];
    [contentView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_offset(25);
        make.right.mas_offset(-25);
        make.centerY.mas_offset(0);
    }];
    
    QMUILabel *titleLab = [QMUILabel labelWithText:self.titleStr font:kSYSTEMFONT(16) textColor:UIColor.blackColor];
    titleLab.numberOfLines = 0;
    [contentView addSubview:titleLab];
    [titleLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_offset(15);
        make.right.mas_offset(-15);
        make.top.mas_offset(15);
    }];
    
    UIView *titleView = titleLab;
    if (self.type == RHAlertTypeTipsSubTitle) {
        QMUILabel *subTitleLab = [QMUILabel labelWithText:self.subTitleStr font:kSYSTEMFONT(14) textColor:kCOLOR_HEX(@"#171A1D")];
        subTitleLab.numberOfLines = 0;
        [contentView addSubview:subTitleLab];
        [subTitleLab mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_offset(15);
            make.right.mas_offset(-15);
            make.top.equalTo(titleLab.mas_bottom).offset(15);
        }];
        titleView = subTitleLab;
    }
    
    UIView *tempView = titleView;
    if (self.type == RHAlertTypeTextView) {
        QMUITextView *textView = [[QMUITextView alloc]init];
        textView.backgroundColor = UIColor.whiteColor;
        textView.layer.cornerRadius = 8;
        textView.layer.masksToBounds = YES;
        textView.maximumTextLength = 64;
        [contentView addSubview:textView];
        self.textView = textView;
        [textView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(titleLab.mas_bottom).offset(15);
            make.left.right.equalTo(titleLab);
            make.height.offset(80);
        }];
        
        _numberLab = [QMUILabel labelWithText:[NSString stringWithFormat:@"0/%ld",self.textMaxLengh] font:kSYSTEMFONT(10) textColor:kRGB(90, 90, 90)];
        [contentView addSubview:_numberLab];
        [_numberLab mas_makeConstraints:^(MASConstraintMaker *make) {
            make.bottom.right.equalTo(textView).offset(-8);
        }];
        
        [[RACSignal merge:@[textView.rac_textSignal,RACObserve(textView, text)]]subscribeNext:^(NSString  *_Nullable text) {
            weakSelf.numberLab.text = [NSString stringWithFormat:@"%ld/%ld",weakSelf.textView.text.length,self.textMaxLengh];
        }];
        tempView = textView;
    }
    
    
    QMUIButton *cancelBtn = [[QMUIButton alloc]init];
    [cancelBtn setTitle:@"取消".rh_localized forState:UIControlStateNormal];
    cancelBtn.titleLabel.font = kSYSTEMFONT(16);
    [cancelBtn setTitleColor:UIColor.blackColor forState:UIControlStateNormal];
    [cancelBtn setCornerRadius:8];
    cancelBtn.backgroundColor = UIColor.whiteColor;
    [contentView addSubview:cancelBtn];
    
    QMUIButton *sureBtn = [[QMUIButton alloc]init];
    [sureBtn setTitle:@"确定".rh_localized forState:UIControlStateNormal];
    sureBtn.titleLabel.font = kSYSTEMFONT(16);
    [sureBtn setTitleColor:UIColor.whiteColor forState:UIControlStateNormal];
    [sureBtn setCornerRadius:8];
    sureBtn.backgroundColor = CMainColorGreen;
    [contentView addSubview:sureBtn];
    
    self.cancelBtn = cancelBtn;
    self.sureBtn   = sureBtn;
    
    [cancelBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(titleLab);
        make.top.equalTo(tempView.mas_bottom).offset(25);
        make.height.offset(45);
        make.bottom.mas_offset(-15);
    }];
    
    [sureBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(cancelBtn.mas_right).offset(15);
        make.right.equalTo(titleLab);
        make.top.width.height.equalTo(cancelBtn);
    }];
    
    
   
    [[cancelBtn rac_signalForControlEvents:UIControlEventTouchUpInside]subscribeNext:^(__kindof UIControl * _Nullable x) {
        [weakSelf hide];
        if(weakSelf.cancelBlock){
            weakSelf.cancelBlock();
        }
    }];
    
    [[sureBtn rac_signalForControlEvents:UIControlEventTouchUpInside]subscribeNext:^(__kindof UIControl * _Nullable x) {
        
        if (weakSelf.type == RHAlertTypeTips||weakSelf.type == RHAlertTypeTipsSubTitle) {
            if (weakSelf.sureBlock) {
                [weakSelf hide];
                weakSelf.sureBlock(@"");
            }
        }
        if (weakSelf.type == RHAlertTypeTextView) {
            if(weakSelf.sureBlock&&weakSelf.textView.text.length>1){
                [weakSelf hide];
                weakSelf.sureBlock(weakSelf.textView.text);
            }
        }
       
        
    }];
    
   
}

-(void)setDefaultText:(NSString*)text
{
    self.textView.text = text;
}

-(void)show
{
    [[UIApplication sharedApplication].keyWindow addSubview:self];
}

-(void)hide
{
    [self removeFromSuperview];
}

@end
